namespace CIS.Resource
{
    using CISLib;
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class ResourceHotelPageEdit : Page
    {
        protected Button AddBtn;
        protected DropDownList DayDdl;
        protected DropDownList DayToDdl;
        protected TextBox HotelAddressTxb;
        protected DropDownList HotelCityDdl;
        protected TextBox HotelFaxTxb;
        protected TextBox HotelFeatureTxb;
        protected TextBox HotelNameTxb;
        protected TextBox HotelPhoneTxb;
        protected DropDownList HotelProvinceDdl;
        protected DropDownList HotelRecommendationRateDdl;
        protected DropDownList HotelRoomTypeDdl;
        protected DropDownList HotelStarDdl;
        protected TextBox HotelUnitPriceTxb;
        protected TextBox HotelWebsiteTxb;
        protected Image iResource;
        protected Label lbEventsTime;
        protected Label lbFrom;
        protected Label lbFromDay;
        protected Label lbFromMonth;
        protected Label lbFromYear;
        protected Label lbHotelAddressTxb;
        protected Label lbHotelCityDdl;
        protected Label lbHotelFaxTxb;
        protected Label lbHotelFeature;
        protected Label lbHotelName;
        protected Label lbHotelPhoneTxb;
        protected Label lbHotelProvinceDdl;
        protected Label lbHotelRecommendationRate;
        protected Label lbHotelRoomType;
        protected Label lbHotelStar;
        protected Label lbHotelWebsite;
        protected Label lbTo;
        protected Label lbToDay;
        protected Label lbToMonth;
        protected Label lbToYear;
        protected Label lbUnit;
        protected Label lbUnitPrice;
        protected DropDownList MonthDdl;
        protected DropDownList MonthToDdl;
        protected UserPrincipal Principal = null;
        protected TextBox RoomPriceTxb;
        protected Button SaveBtn;
        protected TextBox YearToTxb;
        protected TextBox YearTxb;

        private void AddBtn_Click(object sender, EventArgs e)
        {
            string str = this.RoomPriceTxb.Text;
            string str2 = str + this.HotelRoomTypeDdl.SelectedItem.Text + ":" + this.HotelUnitPriceTxb.Text.Trim() + "元; ";
            this.RoomPriceTxb.Text = str2;
        }

        private void FillPage()
        {
            if (base.Request.QueryString["IsModify"] == "True")
            {
                Guid id = new Guid(base.Request.QueryString["HotelId"]);
                ResourceHotel resourceHotel = ManageResource.GetResourceHotelInfoViaId(id);
                this.HotelNameTxb.Text = resourceHotel.HotelName;
                this.HotelUnitPriceTxb.Text = resourceHotel.HotelUnitPrice;
                this.HotelPhoneTxb.Text = resourceHotel.HotelPhone;
                this.HotelFaxTxb.Text = resourceHotel.HotelFax;
                this.HotelWebsiteTxb.Text = resourceHotel.HotelWebsite;
                this.HotelAddressTxb.Text = resourceHotel.HotelAddress;
                this.RoomPriceTxb.Text = resourceHotel.HotelRoomPrice;
                this.HotelFeatureTxb.Text = resourceHotel.HotelFeature;
                if (resourceHotel.HotelDateTo.Year.ToString() != "1800")
                {
                    this.YearToTxb.Text = resourceHotel.HotelDateTo.Year.ToString();
                }
                if (resourceHotel.HotelDateFrom.Year.ToString() != "1800")
                {
                    this.YearTxb.Text = resourceHotel.HotelDateFrom.Year.ToString();
                }
                for (int i = 0; i < this.HotelProvinceDdl.Items.Count; i++)
                {
                    if (this.HotelProvinceDdl.Items[i].Value == resourceHotel.HotelProvince.ToString())
                    {
                        this.HotelProvinceDdl.SelectedIndex = i;
                        break;
                    }
                }
                this.HotelCityDdl.DataSource = ManageCity.GetCityViaProvinceId(resourceHotel.HotelProvince);
                this.HotelCityDdl.DataTextField = "regionName";
                this.HotelCityDdl.DataValueField = "regionId";
                this.HotelCityDdl.DataBind();
                bool result = false;
                for (int i = 0; i < this.HotelCityDdl.Items.Count; i++)
                {
                    if (this.HotelCityDdl.Items[i].Value == resourceHotel.HotelCity.ToString())
                    {
                        this.HotelCityDdl.SelectedIndex = i;
                        result = true;
                        break;
                    }
                }
                if (!result)
                {
                    this.HotelCityDdl.Items.Clear();
                }
                for (int i = 0; i < this.HotelRoomTypeDdl.Items.Count; i++)
                {
                    if (this.HotelRoomTypeDdl.Items[i].Text == resourceHotel.HotelRoomType)
                    {
                        this.HotelRoomTypeDdl.SelectedIndex = i;
                        break;
                    }
                }
                for (int i = 0; i < this.HotelRecommendationRateDdl.Items.Count; i++)
                {
                    if (this.HotelRecommendationRateDdl.Items[i].Value == resourceHotel.HotelRecommendationRate)
                    {
                        this.HotelRecommendationRateDdl.SelectedIndex = i;
                        break;
                    }
                }
                for (int j = 0; j < this.HotelStarDdl.Items.Count; j++)
                {
                    if (this.HotelStarDdl.Items[j].Value == resourceHotel.HotelStar)
                    {
                        this.HotelStarDdl.SelectedIndex = j;
                        break;
                    }
                }
                if ((resourceHotel.HotelDateFrom.Month == 1) && (resourceHotel.HotelDateFrom.Day == 1))
                {
                    this.MonthDdl.SelectedIndex = 0;
                    this.DayDdl.SelectedIndex = 0;
                }
                else
                {
                    for (int i = 0; i < this.MonthDdl.Items.Count; i++)
                    {
                        if (this.MonthDdl.Items[i].Value == resourceHotel.HotelDateFrom.Month.ToString())
                        {
                            this.MonthDdl.SelectedIndex = i;
                        }
                    }
                    for (int i = 0; i < this.DayDdl.Items.Count; i++)
                    {
                        if (this.DayDdl.Items[i].Value == resourceHotel.HotelDateFrom.Day.ToString())
                        {
                            this.DayDdl.SelectedIndex = i;
                        }
                    }
                }
                if ((resourceHotel.HotelDateTo.Month == 1) && (resourceHotel.HotelDateTo.Day == 1))
                {
                    this.MonthToDdl.SelectedIndex = 0;
                    this.DayToDdl.SelectedIndex = 0;
                }
                else
                {
                    for (int i = 0; i < this.MonthToDdl.Items.Count; i++)
                    {
                        if (this.MonthToDdl.Items[i].Value == resourceHotel.HotelDateTo.Month.ToString())
                        {
                            this.MonthToDdl.SelectedIndex = i;
                        }
                    }
                    for (int i = 0; i < this.DayToDdl.Items.Count; i++)
                    {
                        if (this.DayToDdl.Items[i].Value == resourceHotel.HotelDateTo.Day.ToString())
                        {
                            this.DayToDdl.SelectedIndex = i;
                        }
                    }
                }
            }
        }

        private void HotelProvinceDdl_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.HotelCityDdl.DataSource = ManageCity.GetCityViaProvinceId(new Guid(this.HotelProvinceDdl.SelectedItem.Value));
            this.HotelCityDdl.DataTextField = "regionName";
            this.HotelCityDdl.DataValueField = "regionId";
            this.HotelCityDdl.DataBind();
        }

        private void InitializeComponent()
        {
            this.AddBtn.Click += new EventHandler(this.AddBtn_Click);
            this.HotelProvinceDdl.SelectedIndexChanged += new EventHandler(this.HotelProvinceDdl_SelectedIndexChanged);
            this.SaveBtn.Click += new EventHandler(this.SaveBtn_Click);
            base.Load += new EventHandler(this.Page_Load);
        }

        private void InitPage()
        {
            this.SetFromImage();
            this.SetFrom();
            this.HotelRoomTypeDdl.Items.Clear();
            ListItem temItem1 = new ListItem();
            temItem1.Text = "单人间";
            ListItem temItem2 = new ListItem();
            temItem2.Text = "双人间";
            ListItem temItem3 = new ListItem();
            temItem3.Text = "套房";
            ListItem temItem4 = new ListItem();
            temItem4.Text = "豪华套房";
            this.HotelRoomTypeDdl.Items.Add(temItem1);
            this.HotelRoomTypeDdl.Items.Add(temItem2);
            this.HotelRoomTypeDdl.Items.Add(temItem3);
            this.HotelRoomTypeDdl.Items.Add(temItem4);
            this.HotelProvinceDdl.DataSource = ManageProvince.GetProvinceNoAll();
            this.HotelProvinceDdl.DataTextField = "RegionName";
            this.HotelProvinceDdl.DataValueField = "RegionId";
            this.HotelProvinceDdl.DataBind();
            this.HotelCityDdl.DataSource = ManageCity.GetCityViaProvinceId(new Guid(this.HotelProvinceDdl.SelectedItem.Value));
            this.HotelCityDdl.DataTextField = "regionName";
            this.HotelCityDdl.DataValueField = "regionId";
            this.HotelCityDdl.DataBind();
            this.HotelStarDdl.Items.Clear();
            this.HotelRecommendationRateDdl.Items.Clear();
            string stri = "";
            for (int i = 1; i <= 5; i++)
            {
                ListItem item = new ListItem();
                item.Value = i.ToString();
                stri = stri + "*";
                item.Text = stri;
                this.HotelRecommendationRateDdl.Items.Add(item);
            }
            stri = "";
            for (int i = 1; i <= 5; i++)
            {
                ListItem item = new ListItem();
                item.Value = i.ToString();
                stri = stri + "*";
                item.Text = stri;
                this.HotelStarDdl.Items.Add(item);
            }
            this.MonthDdl.Items.Clear();
            this.DayDdl.Items.Clear();
            ListItem tmpItem1 = new ListItem();
            tmpItem1.Text = "--";
            this.MonthDdl.Items.Add(tmpItem1);
            this.DayDdl.Items.Add(tmpItem1);
            for (int i = 1; i <= 12; i++)
            {
                ListItem MonthItem = new ListItem();
                MonthItem.Text = i.ToString();
                this.MonthDdl.Items.Add(MonthItem);
            }
            for (int i = 1; i <= 0x1f; i++)
            {
                ListItem DateItem = new ListItem();
                DateItem.Text = i.ToString();
                this.DayDdl.Items.Add(DateItem);
            }
            this.MonthToDdl.Items.Clear();
            this.DayToDdl.Items.Clear();
            ListItem tmpToItem1 = new ListItem();
            tmpToItem1.Text = "--";
            this.MonthToDdl.Items.Add(tmpToItem1);
            this.DayToDdl.Items.Add(tmpToItem1);
            for (int i = 1; i <= 12; i++)
            {
                ListItem MonthItem = new ListItem();
                MonthItem.Text = i.ToString();
                this.MonthToDdl.Items.Add(MonthItem);
            }
            for (int i = 1; i <= 0x1f; i++)
            {
                ListItem DateItem = new ListItem();
                DateItem.Text = i.ToString();
                this.DayToDdl.Items.Add(DateItem);
            }
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
            this.Principal = this.Context.User as UserPrincipal;
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                this.SelectAuth();
            }
        }

        private void SaveBtn_Click(object sender, EventArgs e)
        {
            DateTime agreementFrom;
            DateTime agreementTo;
            string strHotelName = this.HotelNameTxb.Text.Trim();
            string strHotelUnitPrice = this.HotelUnitPriceTxb.Text.Trim();
            string strHotelPhone = this.HotelPhoneTxb.Text.Trim();
            string strHotelFax = this.HotelFaxTxb.Text.Trim();
            string strHotelWebsite = this.HotelWebsiteTxb.Text.Trim();
            string strHotelAddress = this.HotelAddressTxb.Text.Trim();
            string strRoomPrice = this.RoomPriceTxb.Text;
            string strHotelFeatureTxb = this.HotelFeatureTxb.Text.Trim();
            try
            {
                agreementFrom = new DateTime(Convert.ToInt32(this.YearTxb.Text.Trim()), Convert.ToInt32(this.MonthDdl.SelectedItem.Text), Convert.ToInt32(this.DayDdl.SelectedItem.Text));
            }
            catch (Exception)
            {
                agreementFrom = new DateTime(0x708, 1, 1);
            }
            try
            {
                agreementTo = new DateTime(Convert.ToInt32(this.YearToTxb.Text.Trim()), Convert.ToInt32(this.MonthToDdl.SelectedItem.Text), Convert.ToInt32(this.DayToDdl.SelectedItem.Text));
            }
            catch (Exception)
            {
                agreementTo = new DateTime(0x708, 1, 1);
            }
            if (base.Request.QueryString["IsModify"] == "True")
            {
                if (strHotelName != "")
                {
                    Guid cityId = new Guid("00000000-0000-0000-0000-000000000001");
                    if (this.HotelCityDdl.Items.Count != 0)
                    {
                        cityId = new Guid(this.HotelCityDdl.SelectedItem.Value);
                    }
                    ManageResource.UpdateResourceHotelInfo(new Guid(base.Request.QueryString["HotelId"]), strHotelName, new Guid(this.HotelProvinceDdl.SelectedItem.Value), cityId, strHotelAddress, strHotelPhone, strHotelFax, strHotelWebsite, this.HotelRecommendationRateDdl.SelectedItem.Value, strRoomPrice, this.HotelStarDdl.SelectedValue, agreementFrom, agreementTo, strHotelFeatureTxb);
                    base.Response.Redirect("~/CIS/Resource/ResourceHotelPage.aspx", true);
                }
                base.Response.Write("<script> alert('Name is Null !') </script>");
            }
            else if (strHotelName != "")
            {
                if (!ManageResource.IsExistSameResourceHotelInfo(strHotelName))
                {
                    Guid cityId = new Guid("00000000-0000-0000-0000-000000000001");
                    if (this.HotelCityDdl.Items.Count != 0)
                    {
                        cityId = new Guid(this.HotelCityDdl.SelectedItem.Value);
                    }
                    ManageResource.InsertNewResourceHotelInfo(strHotelName, new Guid(this.HotelProvinceDdl.SelectedItem.Value), cityId, strHotelAddress, strHotelPhone, strHotelFax, strHotelWebsite, this.HotelRecommendationRateDdl.SelectedItem.Value, strRoomPrice, this.HotelStarDdl.SelectedValue, agreementFrom, agreementTo, strHotelFeatureTxb);
                    base.Response.Redirect("~/CIS/Resource/ResourceHotelPage.aspx", true);
                }
                else
                {
                    base.Response.Write("<script> alert('该信息已存在') </script>");
                }
            }
        }

        private void SelectAuth()
        {
            if (this.Principal.OwnRole.TypeC == 0)
            {
                this.InitPage();
                this.FillPage();
                this.SaveBtn.Visible = false;
            }
            else if (this.Principal.OwnRole.TypeC == 1)
            {
                this.InitPage();
                this.FillPage();
            }
        }

        private void SetFrom()
        {
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/CIS/LanguageConfig.xml");
            this.lbHotelName.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceHotelPageEdit", "lbHotelName", language, path);
            this.lbHotelRoomType.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceHotelPageEdit", "lbHotelRoomType", language, path);
            this.lbUnitPrice.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceHotelPageEdit", "lbUnitPrice", language, path);
            this.lbUnit.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceHotelPageEdit", "lbUnit", language, path);
            if (this.Principal.OwnArea[0].AreaId.ToString() == "a7f26634-10a9-4cb1-aa2c-8c91a8dfdd66")
            {
                this.lbHotelProvinceDdl.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceHotelPageEdit", "lbHotelProvinceDdlOverSea", language, path);
                this.lbHotelCityDdl.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceHotelPageEdit", "lbHotelCityDdlOverSea", language, path);
            }
            else
            {
                this.lbHotelProvinceDdl.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceHotelPageEdit", "lbHotelProvinceDdl", language, path);
                this.lbHotelCityDdl.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceHotelPageEdit", "lbHotelCityDdl", language, path);
            }
            this.lbHotelAddressTxb.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceHotelPageEdit", "lbHotelAddressTxb", language, path);
            this.lbHotelPhoneTxb.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceHotelPageEdit", "lbHotelPhoneTxb", language, path);
            this.lbHotelFaxTxb.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceHotelPageEdit", "lbHotelFaxTxb", language, path);
            this.lbHotelWebsite.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceHotelPageEdit", "lbHotelWebsite", language, path);
            this.lbHotelRecommendationRate.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceHotelPageEdit", "lbHotelRecommendationRate", language, path);
            this.lbHotelFeature.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceHotelPageEdit", "lbHotelFeature", language, path);
            this.SaveBtn.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceHotelPageEdit", "SaveBtn", language, path);
            this.lbHotelStar.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceHotelPageEdit", "lbHotelStar", language, path);
            this.lbFromYear.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceHotelPageEdit", "lbFromYear", language, path);
            this.lbFromMonth.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceHotelPageEdit", "lbFromMonth", language, path);
            this.lbFromDay.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceHotelPageEdit", "lbFromDay", language, path);
            this.lbTo.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceHotelPageEdit", "lbTo", language, path);
            this.lbFrom.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceHotelPageEdit", "lbFrom", language, path);
            this.lbToYear.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceHotelPageEdit", "lbToYear", language, path);
            this.lbToMonth.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceHotelPageEdit", "lbToMonth", language, path);
            this.lbToDay.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceHotelPageEdit", "lbToDay", language, path);
            this.lbEventsTime.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceHotelPageEdit", "lbEventsTime", language, path);
        }

        private void SetFromImage()
        {
            switch (this.Session["language"].ToString())
            {
                case "cn":
                    this.iResource.ImageUrl = "../images/title_SharedInfo.jpg";
                    break;

                case "en":
                    this.iResource.ImageUrl = "../images/title_SharedInfoEn.jpg";
                    break;
            }
        }
    }
}

